// package-star/pages/brand-wxpay-applyment/index.js
import { ManageTargetInfoUpdateMixins, ManageTargetExchangeMixins } from '../../../behaviors/page';
import { navBack, getImageInfo, readFile, showLoading, hideLoading, tipSuccess } from '../../../common/env';
import { isEmpty } from '../../../common/utils';
import { TARGET_TYPE } from '../../../constants/common';
import { COMMON_PAGE } from '../../../constants/route';
import { WXPAYMENT_ORGANIZATION_TYPE_VALUE } from '../../../constants/payment';
import { dealWxpayPaymentDefaultData } from '../../../biztools/manage';
import { getWxpayApplyDetail, editWxpayApply, uploadWxpayApplyMedia } from '../../../api/brd';
import { getQRcode } from '../../../api/common';

Page({
    mixins: [ManageTargetInfoUpdateMixins, ManageTargetExchangeMixins],
    data: {
        WXPAYMENT_ORGANIZATION_TYPE_VALUE,
        steps: [
            { name: '选择开通类型'},
            { name: '填写资料'},
            { name: '审核结果'},
        ],
        step: 0,
        wxpayApplymentId: '',
        applyForm: {},
        applyResult: {},
        inited: false,
    },
    async onLoad(options) {
        const { brandId } = options;
        await this._handleExchange(TARGET_TYPE.brand, brandId);
        await this._handleGetDetail();
    },
    async _handleGetSalesScene(isApply) {
        let salesSceneInfo = undefined;
        console.warn("isApply =>", isApply)
        if (!isApply) {
            const { manageTargetInfo } = this.data;
            const url = await getQRcode({
                page: COMMON_PAGE['shareBrand'].replace(/^\//, ''),
                scene: `brandId=${manageTargetInfo.brandId}`,
            });
            const fileInfo = await getImageInfo(url);
            const base64 = await readFile(fileInfo.path);
            const res = await uploadWxpayApplyMedia({ image: base64.data, imageType: fileInfo.type === 'jpeg' ? 'jpg' : fileInfo.type });
            salesSceneInfo = {
                storeName: manageTargetInfo.name,
                storeQrCode: res.mediaId,
            }
        }
        return salesSceneInfo;
    },
    async _handleGetDetail(force) {
        showLoading(force ? '刷新中...' : '加载中...');
        const { applyForm, applyResult } = this.data;
        const detail = await getWxpayApplyDetail();
        const isApply = !isEmpty(detail);
        const { wxpayApplymentId, applyNo, wxApplymentId, applyState, applyStateDesc, auditDetails, signUrl, legalValidationUrl } = detail;
        const applyFormData = isApply ? detail.applyFormData : dealWxpayPaymentDefaultData();
        const salesSceneInfo = await this._handleGetSalesScene(isApply);
        console.log('salesSceneInfo=>', salesSceneInfo)
        const salesSceneInfoWrapper = salesSceneInfo ? { salesSceneInfo } : {};
        this.setData({
            inited: true,
            step: isApply ? 2 : 0,
            wxpayApplymentId,
            applyForm: {
                ...applyForm,
                ...applyFormData,
                ...salesSceneInfoWrapper,
            },
            applyResult: {
                ...applyResult,
                applyNo,
                wxApplymentId,
                applyState,
                applyStateDesc,
                auditDetails,
                signUrl,
                legalValidationUrl,
            },
        }, () => {
            console.log('setDatacallback', this.data.applyForm)
        });
        hideLoading();
    },
    handleSelectType(e) {
        const type = e.detail;
        this.setData({
            step: 1,
            'applyForm.organizationType': type,
        });
    },
    handleFormPrev(e) {
        this.setData({
            step: 0,
        });
    },
    async handleFormNext(e) {
        const { wxpayApplymentId } = this.data;
        const applyForm = e.detail;
        showLoading('保存中...');
        const flag = await editWxpayApply({
            wxpayApplymentId,
            wxpayApplyFormData: applyForm
        });
        if (flag) {
            hideLoading();
            tipSuccess('保存成功');
            this._handleGetDetail();
        } else {
            this.setData({ applyForm });
        }
    },
    handleModifyResult() {
        this.setData({ step: 1 });
    },
    handleCompleteResult() {
        navBack();
    },
    handleRefreshResult() {
        this._handleGetDetail(true);
    },
})
